package cn.qf.first.exceptions.handler;

import cn.qf.first.exceptions.BaseException;
import cn.qf.first.pojo.vo.RespVo;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author Ran
 * @since JDK 1.8
 *
 *      全局异常处理类
 *
 */
@ControllerAdvice
@RestControllerAdvice
public class ExceptionsHandler {

    @ExceptionHandler(BaseException.class)
    @ResponseBody
    public RespVo handlerException(BaseException e){
        RespVo respVo = new RespVo();
        //获取异常的状态码
        respVo.setCode(e.getErrorCode());
        //获取异常的异常信息
        respVo.setMsg(e.getMessage());
        return respVo;
    }
}
